﻿using System.Collections.Generic;
using System;

namespace HREngine.Bots
{
    public partial class Behavior防战 : Behavior
    {
        public override string BehaviorName() { return "防战"; }
        PenalityManager penman = PenalityManager.Instance;
        // 核心，场面值
        public override float getPlayfieldValue(Playfield p)
        {
            if (p.value > -20000) return p.value;
            float retval = 0;
            retval += getGeneralVal(p);
            retval += p.owncarddraw * 5;
            // 护甲
            retval += p.ownHero.armor;
            // 危险血线
            int hpboarder = 10;
            // 抢脸血线
            int aggroboarder = 15;
            retval += getHpValue(p, hpboarder, aggroboarder);
            // 对手基本随从交换模拟
            retval += enemyTurnPen(p);
            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;
            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                        continue;
                    case actionEnum.useHeroPower:
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                switch (a.card.card.chnName)
                {
                    case "顺劈斩":
                        retval -= i ;
                        break;
                    case "怒火中烧":
                        if(a.target != null && a.target.handcard.card.chnName == "苦痛侍僧")
                        {
                            retval += 6 - i * 3;
                        }
                        retval += i;
                        break;
                    //case "全副武装！":
                    //    retval -= i;
                    //    break;
                    // 排序优先
                    case "猛击":
                    case "盾牌格挡":
                        retval -= i * 2;
                        break;
                    //case "绝命乱斗":
                    //    if (i != 0) retval -= 50;
                    //    break;
                    case "盾牌猛击":
                        retval += i;
                        break;
                }
            }
            retval += getSecretPenality(p); // 奥秘的影响
            //p.value = retval;
            return retval;
        }


        // 敌方随从价值 主要等于 （HP + Angr） * 4 + 10 
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            if (m.Hp <= 0) return 0;
            bool setGeddon = false;
            foreach (Minion mm in p.ownMinions)
            {
                if (mm.handcard.card.chnName == "迦顿男爵")
                {
                    setGeddon = true;
                    break;
                }
            }
            if(setGeddon && m.Hp < 3 && !m.divineshild)
            {
                return 0;
            }
            int retval = 10;
            retval += m.Hp * 4;
            if (!m.frozen && !(m.cantAttack && m.name != CardDB.cardName.argentwatchman))
            {
                retval += m.Angr * 4;
                if (m.windfury) retval += m.Angr * 2;
                if (m.Angr > 5) retval += (m.Angr - 4) * 10;
            }

            if (m.taunt) retval += 1;
            if (m.divineshild) retval += m.Angr;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 1;

            if (m.poisonous)
            {
                retval += 4;
            }

            // 亡语怪不优先解
            if (m.handcard.card.deathrattle)
            {
                retval -= 3;
            }

            int bonus = 4;
            switch (m.handcard.card.chnName){
                // 异能价值每多 1 属性值加权 4 点
                // 异能价值 4 属性值
                case "加基森拍卖师":
                case "任务达人":
                case "苔原犀牛":
                    retval += bonus * 4;
                    break;
                // 异能价值 3 属性
                case "迦顿男爵":
                case "法力之潮图腾":
                case "火舌图腾":
                case "雷欧克":
                    retval += bonus * 3;
                    break;
                // 异能价值 2 属性
                case "巫师学徒":
                case "飞刀杂耍者":
                case "大法师安东尼达斯":
                case "苦痛侍僧":
                case "北郡牧师":
                case "狂野炎术士":
                case "奥金尼灵魂祭司":
                case "暴风城勇士":
                case "年轻的女祭司":
                case "恐狼前锋":
                case "森林狼":
                case "食腐土狼":
                case "法力浮龙":
                case "饥饿的秃鹫":
                    retval += bonus * 2;
                    break;
                // 异能价值 1 属性
                case "铸甲师":
                case "先知维纶":
                case "炎魔之王拉格纳罗斯":
                    retval += bonus;
                    break;
                case "末日预言者":
                    foreach(Minion mm in p.ownMinions)
                    {
                        retval += mm.Hp + mm.Angr;
                    }
                    if(p.ownMinions.Count == 0)
                    {
                        retval = -20;
                    }
                    break;
                // 大哥，求别解
                case "希尔瓦娜斯":
                    if (p.ownMinions.Count > 1)
                        retval -= 10;
                    else retval += 10;
                    break;                
            }
            return retval;
        }

        // 我方随从价值 主要等于 （HP + Angr） * 3 + 5 
        public override int getMyMinionValue(Minion m, Playfield p)
        {
            if (m.Hp <= 0) return 0;
            bool setGeddon = false;
            foreach (Minion mm in p.ownMinions)
            {
                if (mm.handcard.card.chnName == "迦顿男爵")
                {
                    setGeddon = true;
                    break;
                }
            }
            if (setGeddon && m.Hp < 3 && !m.divineshild)
            {
                return 0;
            }

            int retval = 5;
            retval += m.Hp * 3;
            retval += m.Angr * 3;
            if (m.Hp <= 1) retval -= (m.Angr - 1) * 2;
            // 鱼人种族价值
            //if (m.handcard.card.race == 14)
            //{
            //    retval += m.Hp + m.Angr + 2;
            //}
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild ) retval += m.Angr / 2 + 1;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 3 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 3 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;
            // retval += m.synergy;
            // retval += getSpeicalCardVal(m.handcard.card);

            int bonus = 3;
            switch (m.handcard.card.chnName)
            {
                // 异能价值 4 属性
                case "炎魔之王拉格纳罗斯":
                case "伊瑟拉":
                    retval += bonus * 4;
                    break;
                // 异能价值 2 属性
                case "迦顿男爵":
                case "苦痛侍僧":
                case "战利品贮藏者":
                case "麦田傀儡":
                    retval += bonus * 2;
                    break;
            }

            return retval;
        }

        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }
        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 0 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.totemiccall, 1 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.armorup, 2 },
            { CardDB.cardName.steadyshot, 8 }
        };

        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }
            bool found_aoe = false;
            foreach(Minion m in p.ownMinions)
            {
                if (m.handcard.card.chnName == "迦顿男爵") found_aoe = true;
            }
            // 初始惩罚值
            int pen = 0;
            pen += penman.getSilencePenality(card.name, target, p);
            switch (card.chnName)
            {
                case "幸运币":
                    pen += 5;
                    break;
                case "斩杀":
                    pen += 30;
                    break;
                case "旋风斩":
                    pen += 20;
                    break;
                case "狂奔科多兽":
                    break;
                case "绝命乱斗":
                    pen += 40;
                    break;
                case "猛击":
                    pen += 18;
                    if (found_aoe && target != null && target.Hp <= 2) pen += 1000;
                    break;
                case "严酷的监工":
                case "怒火中烧":
                    pen += 10;
                    if (target != null && target.own && target.Hp > 2 && !target.Ready)
                    {
                        pen += 1000;
                    }
                    break;
                case "迦顿男爵":
                    foreach(Minion m in p.enemyMinions)
                    {
                        if (m.Hp <= 2) pen -= 20;
                    }
                    foreach (Minion m in p.ownMinions)
                    {
                        if (m.Hp <= 2) pen += 10;
                    }
                    break;
                case "格罗玛什·地狱咆哮":
                    pen += 15;
                    break;
                case "顺劈斩":
                    pen += 15;
                    break;
                case "阿古斯防御者":
                    break;
                case "黑骑士":
                    break;
                case "凯恩·血蹄":
                    pen -= 15;
                    break;
                case "希尔瓦娜斯·风行者":
                    if (p.enemyMinions.Count > 3) pen -= 20;
                    break;
               case "炎魔之王拉格纳罗斯":
                    if (p.ownHero.Hp < 10 && p.enemyMinions.Count > 1) pen += 20;
                    break;
                case "盾牌猛击":
                    pen += 25;
                    if(target.Hp > p.ownHero.armor + p.spellpower)
                    {
                        pen += 15;
                    }
                    break;
                case "阿莱克丝塔萨":
                    pen += 20;
                    break;
                //-----------------------------英雄技能-----------------------------------
                case "全副武装！":
                    pen += 5;
                    break;
            }
            return pen;
        }

        public override int getHpValue(Playfield p, int hpboarder, int aggroboarder)
        {
            int retval = 0;
            // 血线安全
            if (p.ownHero.Hp + p.ownHero.armor > hpboarder)
            {
                retval += (5 + p.ownHero.Hp + p.ownHero.armor - hpboarder) ;
            }
            // 快死了
            else
            {
                if (p.nextTurnWin()) retval -= (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
                else retval -= 5 * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor) * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
            }
            if(p.ownHero.Hp + p.ownHero.armor < 10 && p.ownHero.Hp + p.ownHero.armor > 0)
            {
                retval -= 200 / (p.ownHero.Hp + p.ownHero.armor);
            }
            // 对手血线安全,反正都是红龙一口的事情，完全不急
            if (p.enemyHero.Hp + p.enemyHero.armor >= aggroboarder)
            {
                retval += aggroboarder - p.enemyHero.Hp - p.enemyHero.armor ;
            }
            // 开始打脸
            else if (p.enemyHero.Hp + p.enemyHero.armor <= aggroboarder && p.enemyHero.Hp + p.enemyHero.armor > 3)
            {
                retval += 2 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            // 一个小斧子就斩了的事情
            else
            {
                retval += 6 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            return retval;
        }
    }
}